package top.chmin.project.cloud.common.code.template.auto;

// field import auto
// #bind.field.fixLength#

import cn.hutool.core.bean.BeanUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.chmin.project.cloud.common.entity.Page;

import java.util.List;
import java.util.stream.Stream;


/**
 * chmin
 * 2023-12-27 19:56:45
 * cloud-builder
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CodeTemplateService {

    @Resource
    CodeTemplatePlugin plugin;

    @Resource
    CodeTemplateDao codeTemplateDao;

// field labelDao auto

    public int save(CodeTemplateSave.Param param) {
        plugin.checkSave(param);
        CodeTemplate codeTemplate = BeanUtil.toBean(param, CodeTemplate.class);
        plugin.beforeSave(codeTemplate);
        int result = codeTemplateDao.save(codeTemplate);
        plugin.afterSave(param, codeTemplate, result);
        return result;
    }

    public int updateById(CodeTemplateUpdateById.Param param) {
        plugin.checkUpdateById(param);
        CodeTemplate codeTemplate = BeanUtil.toBean(param, CodeTemplate.class);
        plugin.beforeUpdateById(codeTemplate);
        int result = codeTemplateDao.updateById(codeTemplate);
        plugin.afterUpdateById(param, codeTemplate, result);
        return result;
    }

    public int deleteById(CodeTemplateDeleteById.Param param) {
        plugin.checkDeleteById(param);
        int result = codeTemplateDao.deleteById(param.getId());
        plugin.afterDeleteById(param, result);
        return result;
    }

    public CodeTemplateSelectById.Result selectById(CodeTemplateSelectById.Param param) {
        plugin.checkSelectById(param);
        CodeTemplateFPC fpc = new CodeTemplateFPC().id().eq(param.getId());
        plugin.beforeSelectById(param, fpc);
        CodeTemplateSelectById.Result result = codeTemplateDao.getFirst(fpc.toCondition(), CodeTemplateSelectById.Result.class);
        plugin.afterSelectById(param, result);
        return result;
    }

    public Page<CodeTemplateSelectPage.Result> selectPage(CodeTemplateSelectPage.Param param) {
        plugin.checkSelectPage(param);
        CodeTemplateFPC fpc = new CodeTemplateFPC();
        fpc = param.isDesc() ? fpc.id().desc() : fpc.id().asc();
        plugin.beforeSelectPage(param, fpc);
        Page<CodeTemplateSelectPage.Result> result = codeTemplateDao.getPageByCondition(fpc.toCondition(), CodeTemplateSelectPage.Result.class);
// field label auto
        plugin.afterSelectPage(param, result);
        return result;
    }

    // entity select auto
    // entity select auto
    public List<CodeTemplateCodeNameSelect.Result> selectCodeName(CodeTemplateCodeNameSelect.Param param) {
        plugin.checkSelectCodeName(param);
        CodeTemplateFPC fpc = new CodeTemplateFPC();
        plugin.beforeSelectCodeName(param, fpc);
        Stream<CodeTemplateCodeNameSelect.Result> result = codeTemplateDao.getStreamByCondition(fpc.toCondition(), CodeTemplateCodeNameSelect.Result.class)
                .peek(r -> {
                    r.setValue(r.getCodeName());
                    r.setLabel(r.getCodeName());
                });
        List<CodeTemplateCodeNameSelect.Result> list = result.toList();
        plugin.afterSelectCodeName(param, list);
        return list;
    }


// entity search auto

}
